<template>
    <div>
       <Row type="flex" justify="space-between" align="middle">
            <Col span="15" class="title">修改密码</Col>
       </Row>
       <div class="table-box" style="overflow: auto;">
           <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
                <FormItem label="原密码" prop="oldPassword">
                    <Input v-model="formValidate.oldPassword" type="password" placeholder="输入原密码"></Input>
                </FormItem>
                <FormItem label="新密码" prop="newPassword">
                    <Input v-model="formValidate.newPassword" type="password" placeholder="输入新密码"></Input>
                </FormItem>
                <FormItem label="确认密码" prop="surePassword">
                    <Input v-model="formValidate.surePassword" type="password" placeholder="输入确认密码"></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="handleSubmit('formValidate')">提交修改</Button>
                </FormItem>
            </Form>
       </div>
    </div>
</template>
<script>
    import md5 from 'md5'

    export default {
        data () {

           return {
                formValidate: {
                    oldPassword: '',
                    newPassword: '',
                    surePassword: ''
                },
                ruleValidate: {
                    oldPassword: [
                        { required: true, message: '不能为空！', trigger: 'blur' },
                        { type: 'string', min: 5, message: '字数不能少于5！', trigger: 'blur' }
                    ],
                    newPassword: [
                        { required: true, message: '不能为空！', trigger: 'blur' },
                        { type: 'string', min: 5, message: '字数不能少于5！', trigger: 'blur' }
                    ],
                    surePassword: [
                        { required: true, message: '不能为空！', trigger: 'blur' },
                        { type: 'string', min: 5, message: '字数不能少于5！', trigger: 'blur' }
                    ]
                }
           }
        },
        created: function () {
        },
        methods: {
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        var that = this;

                        // 判断输入的两次新密码是否一致
                        if(that.formValidate.newPassword !== that.formValidate.surePassword) {
                            that.$Message.info('两次新密码不相同，请重新输入！');
                        } else {
                            that.$loading.show('更新中');
                            that.$util.ajax({
                                that: that,
                                url: '/admin/pwd/',
                                data: {
                                    oldPassword: md5(that.formValidate.oldPassword),
                                    newPassword: md5(that.formValidate.newPassword)
                                },
                                success: function(data){
                                    that.$Message.info(data.msg);
                                    that.$router.go(-1);
                                },
                                fail: function(msg){
                                    that.$Message.info(msg);
                                },
                                complete: function(){
                                    that.$loading.hide();
                                }
                            });
                        }
                    }
                })
            }
        }
    }
</script>
<style scoped>

</style>